﻿using System;
using System.IO;
using System.Linq;

public partial class Reference : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int pageIndex = 1;
        if (Request.QueryString["PageIndex"] != null)
        {
            bool result = Int32.TryParse(Request.QueryString["PageIndex"].ToString(), out pageIndex);
            if (!result)
                pageIndex = 1;
        }

        BuildGallery(pageIndex);

    }

    private void BuildGallery(int pageIndex)
    {
        string path = Server.MapPath("images\\Sirketler");
        string[] filePaths = Directory.GetFiles(path);
        const int maxImagePerPage = 15;
        int startIndex = 0;
        int endIndex = 0;

        startIndex = (pageIndex - 1) * maxImagePerPage;
        endIndex = startIndex + maxImagePerPage;


        string innerHtml = "<table align=\"center\"><tr><td><ul class=\"hoverbox\">";
        string imagePath;
        for (int i = startIndex; i < filePaths.Count() && i < endIndex; i++)
        {

            imagePath = filePaths[i].Substring(filePaths[i].IndexOf("images\\"));
            innerHtml +=
            "<li><a href=\"#\"><img src=\"" + imagePath + "\" alt=\"description\" /><img src=\"" + imagePath + "\" alt=\"description\" class=\"preview\" /></a> </li>";
        }



        innerHtml += "</ul></td></tr>";
        

        //innerHtml = "";
        decimal asad = maxImagePerPage;
        int maxPageNumber = Convert.ToInt32(Math.Ceiling(filePaths.Count() / asad));
        innerHtml += "<tr align=\"center\"><td><a href=\"Reference.aspx?PageIndex=1\">1</a>";
        for (int i = 2; i <= maxPageNumber; i++)
        {
            innerHtml += "<a href=\"Reference.aspx?PageIndex=" + i + "\"> - " + i + "</a>";
        }
        innerHtml += "</td></tr></table>";
        gallery.InnerHtml = innerHtml;
    }
}